package com.zx.kitchen.index.common.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.zx.kitchen.index.R;
import com.zx.kitchen.index.common.activity.NewBusinessInfoActivity;
import com.zx.kitchen.index.common.adapter.CateListAdapter;
import com.zx.kitchen.index.common.adapter.CateTreeListViewAdapter;
import com.zx.kitchen.index.common.entity.CateListBean;
import com.zx.kitchen.index.common.entity.DishesListBean;
import com.zx.kitchen.index.common.entity.GoodsReportDishesBean;
import com.zx.kitchen.index.common.entity.IndexOrderBean;
import com.zx.kitchen.index.common.entity.NextDishesListBean;
import com.zx.kitchen.index.common.mvp.presenter.BusinessGoodsReportPersenter;
import com.zx.kitchen.index.common.mvp.presenter.IndexOrderNextPresenter;
import com.zx.kitchen.index.common.mvp.presenter.NewBusinessPresenter;
import com.zx.kitchen.index.common.mvp.presenter.NewPurchasePresenter;
import com.zx.kitchen.index.common.mvp.presenter.PurchaseGoodsReportPersenter;
import com.zx.kitchen.index.common.mvp.view.IndexOrderNextView;
import com.zx.kitchen.index.common.mvp.view.NewBusinessView;
import com.zx.kitchen.index.common.utils.treeviewutil.Node;
import com.zx.kitchen.index.common.utils.treeviewutil.adapter.TreeListViewAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/5.
 */

public class CateListFragment extends Fragment implements NewBusinessView, IndexOrderNextView {
    private ListView listView;
    private List<CateListBean> cateList = new ArrayList<>();
    private List<DishesListBean> dishesList = new ArrayList<>();
    private NewBusinessPresenter newBusinessPresenter;
    private NewPurchasePresenter newPurchasePresenter;
    private Context context;
    private IndexOrderNextPresenter indexOrderNextPresenter;
    private CateListAdapter adapter;
    private boolean isPurchase = false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_catelist, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        context = getContext();
        newBusinessPresenter = new NewBusinessPresenter(this, context);
        newPurchasePresenter = new NewPurchasePresenter(this, context);
        indexOrderNextPresenter = new IndexOrderNextPresenter(this, context);
        NewBusinessInfoActivity activity = (NewBusinessInfoActivity) getActivity();
        isPurchase = activity.isPurchase;
        initView(view);
    }

    private void initView(View view) {
        listView = (ListView) view.findViewById(R.id.catelist);
        TextView tvSelectAll = (TextView) view.findViewById(R.id.tv_select_all);
        tvSelectAll.setVisibility(View.GONE);
        if (isPurchase) {
            newPurchasePresenter.getFromNet();
        } else {
            newBusinessPresenter.getFromNet();
        }
        adapter = new CateListAdapter(context, cateList);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setSelect(position);
                if (mListener != null) {
                    mListener.dataTransmission(cateList.get(position));//传递需要展示的数据
                }

//                if (cateList.get(position).getIs_last() == 0) {//不是最后一级还要获取数据
//                    indexOrderNextPresenter.getFromNet(cateList.get(position).getFid(), cateList.get(position).getIs_last());
//                }

            }
        });
    }

    public OnDataTransmissionListener mListener;

    //接口回调的方法
    public interface OnDataTransmissionListener {
        //新建营业信息 传递数据用
        void dataTransmission(CateListBean cateListBean);
    }

    public void setOnDataTransmissionListener(OnDataTransmissionListener mListener) {
        this.mListener = mListener;
    }


    @Override
    public void onSuccess(IndexOrderBean list) {
        dishesList.clear();
        cateList.addAll(list.getCate_list());
        dishesList.addAll(list.getDishes_list());

        if (mListener != null) {
            //在第一次进入时右侧显示数据
            mListener.dataTransmission(cateList.get(0));//传递需要展示的数据
        }
        adapter.notifyDataSetChanged();

    }

    @Override
    public void onFail(String str) {

    }


}

